package com.example.administrator.capital.mine.second;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;
import com.example.administrator.capital.R;
import com.example.administrator.capital.base.BaseBackFragment;
import com.example.administrator.capital.eventbus.EventBase;
import com.example.administrator.capital.presenter.ItemsPresenter;
import com.example.administrator.capital.presenter.base.IViewRequest;
import com.example.administrator.capital.utils.PreferenceManager;
import com.lzy.okgo.model.Response;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import eventbusactivityscope.EventBusActivityScope;

public class RechargeWebFragment extends BaseBackFragment {

    @BindView(R.id.web_view)
    WebView mWebView;
    Unbinder unbinder;
    private TextView mBack;
    private String rechargeUrl="";
    private ItemsPresenter mItemsPresenter;
    public RechargeWebFragment() {
        // Required empty public constructor
    }

    public static RechargeWebFragment newInstance(String s) {
        Bundle args = new Bundle();
        args.putString("money",s);
        RechargeWebFragment fragment = new RechargeWebFragment();
        fragment.setArguments(args);
        return fragment;
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_recharge_web, container, false);
        unbinder = ButterKnife.bind(this, view);
        return attachToSwipeBack(view);
    }
    @Override
    protected void initView(View view) {
        super.initView(view);

        rechargeUrl="http://www.zhongwangzb.com/chuangfuCopy/app/onlinepay?uid="+ PreferenceManager.instance().getUseId()+"&&tranAmt="+mBundle.getString("money");
        mItemsPresenter = new ItemsPresenter(null);
        mBack = view.findViewById(R.id.app_title_back);
        mWebView.loadUrl(rechargeUrl);
    }

    @Override
    protected void setListener() {
        super.setListener();
        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                _mActivity.onBackPressed();
            }
        });
    }

    @Override
    protected void setData() {
        super.setData();
        WebSettings webSettings = mWebView.getSettings();
        // 设置与Js交互的权限
        webSettings.setJavaScriptEnabled(true);
        // 设置允许JS弹窗
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);

        mWebView.setWebChromeClient(new WebChromeClient() {
        });
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
            }
        });
        mWebView.setWebViewClient(new WebViewClient(){
                                     @Override
                                     public boolean shouldOverrideUrlLoading(WebView view, String url) {
                                         if(url == null) return false;
                                           if (url.contains("app/homeurl")||url.contains("app/reurl")) {
                                               _mActivity.onBackPressed();
                                             }
                                         //处理http和https开头的url
                                         mWebView.loadUrl(url);
                                         return true;
                                     }
                                 });
//        getData(PreferenceManager.instance().getUseId(),mBundle.getString("money"));
    }
    private void getData(String uid, String tranAmt) {
        mItemsPresenter.onlinepay(new IViewRequest<String>() {
            @Override
            public void onFailMsg(String var) {

            }
            @Override
            public void onCode(int code) {
            }
            @Override
            public void onSuccess(Response<String> data) {
               mWebView.loadUrl(rechargeUrl);
            }
            @Override
            public void onFail(Response<String> error) {
            }
        }, uid, tranAmt);
    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBusActivityScope.getDefault(getActivity()).post(new EventBase("刷新界面"));
        unbinder.unbind();
    }
}
